# -*-coding:Utf-8 -*
""" UI Input Text Class
"""
import bge, bgl, blf
from .textbox import *
from .buttonlist import *
from ..common.events import *
from ..inputs import *

class InputField(TextBox, TextInput, BasicEvent):
	def __init__(self, parent,x, y, text="", inputMap=False, onchange=False, **kargs):
		TextBox.__init__(self, parent,x, y, text)
		TextInput.__init__(self, inputMap)		
		self._font=ButtonList.DefaultFont
		self._size=ButtonList.DefaultSize
		self.focused=False
		BasicEvent.__init__(self)		
		self._onclic=self.focus
		self._onchange=onchange
		self.focBlur=False
		self._log=text
		self.oldLog=text
		self._cursor=len(text)
	
	def getText(self):
		return self._log
	
	def update(self, value=False):
		if value:
			TextInput.update(self, value)
			if self.focBlur:
				self._text=self._log[:self._cursor]+'|'+self._log[self._cursor:]
			else:
				self._text=self._log[:self._cursor]+' '+self._log[self._cursor:]
			if self.oldLog!=self._log:
				if self._onchange:
					self._onchange()
				self.oldLog=self._log
		else:
			if self.TimeFromLastUpdate<0.5:
				return True
			else:
				if self.focBlur:
					self._text=self._log[:self._cursor]+' '+self._log[self._cursor:]
					self.focBlur=False
				else:
					self._text=self._log[:self._cursor]+'|'+self._log[self._cursor:]
					self.focBlur=True
	
	def focus(self, *params):
		self.activate()
